package com.study.controller;

import java.math.BigDecimal;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Payplan;
import com.study.model.pojo.Payrecord;
import com.study.model.pojo.Scheduleform;
import com.study.services.PayPlanService;
import com.study.services.PayRecordService;
import com.study.services.ScheduleFormService;

@Controller
public class PayPlanController {
	@Autowired
	ScheduleFormService service;
	@Autowired
	PayPlanService pps;
	@Autowired
	PayRecordService prs;
	
	/**
	 * 跳转付款计划
	 * @return
	 */
	@RequestMapping("/payAdd")
	public ModelAndView pay(HttpSession session) {
		String schno = (String) session.getAttribute("schid");
		JSONObject da = service.findById(schno);
		System.out.println("付款计划有这个值吗？"+schno);
		ModelAndView mv = new ModelAndView("/goods/pay");
		mv.addObject("da", da);
		return mv;
	}
	
	/**
	 * 付款
	 * @return
	 */
	@RequestMapping("/gopay")
	public ModelAndView payed(HttpSession session) {
		String schno = (String) session.getAttribute("schid");
		JSONObject da = service.findById(schno);
		System.out.println("付款有这个值吗？"+schno);
		ModelAndView mv = new ModelAndView("/goods/gopay");
		mv.addObject("da", da);
		return mv;
	}
	
	
	/**
	 * 添加付款计划
	 * @param schno
	 * @param userid
	 * @param money
	 * @param date1
	 * @param remark
	 * @param way
	 * @param pay
	 * @param session
	 */
	@PostMapping("/addpayplan")
	@ResponseBody
	public void add(Integer schno,Integer userid,BigDecimal money,Date date1,String remark,
					HttpSession session) {
		System.out.println("采购单："+schno+"\n负责人："+userid+"\n总金额："+money+"\n付款时间："+date1);
		session.setAttribute("sch", String.valueOf(schno));
		//添加付款计划
		Payplan p = new Payplan();
		//JSONObject plan = service.sele(schno);
		Scheduleform sf=new Scheduleform();
		sf.setSchno(schno); 
		
		p.setScheduleform(sf);
		p.setUserid(userid);
		p.setPayperiod(3000);
		p.setPaytime(date1);
		p.setPaynum(money);
		p.setRemark(remark);
		pps.insert(p);
	}
	
	
	/**
	 * 添加付款记录
	 * @param schno
	 * @param money
	 * @param way
	 */
	@PostMapping("/addpayrecord")
	@ResponseBody
	public void addRecord(Integer schno,BigDecimal money,Integer way) {
		Payrecord r = new Payrecord();
		JSONObject plan = pps.selectplan(schno);
		//json转实体类
		Payplan p = plan.toJavaObject(plan, Payplan.class);
		pps.updatepay(p.getPayid());
		System.out.println("是否付款修改了吗"+p.getPayperiod());
		r.setPayplan(p);
		r.setParway(way);
		r.setPartime(new Date());
		prs.insert(r);
		//修改已付款金额
		service.updatepay(schno,money);
	}
}
